//
//  NYCommentEditView.m
//  iMobile
//
//  Created by baken on 2017/1/18.
//  Copyright © 2017年 MyApp. All rights reserved.
//

#import "NYCommentEditView.h"


@interface NYCommentEditView()<UITextViewDelegate>

@property (nonatomic, strong)UIButton *sendButton;

@end

@implementation NYCommentEditView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColorFromRGB(0xf5f5f5);
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews{
    UITextView *textView = [[UITextView alloc] init];
    textView.delegate = self;
    textView.font = [UIFont systemFontOfSize:14];
    textView.backgroundColor = [UIColor whiteColor];
    textView.layer.borderColor = UIColorFromRGB(0xe4e4e4).CGColor;
    textView.layer.borderWidth = 0.5;
    textView.layer.masksToBounds = YES;
    textView.layer.cornerRadius = 8;
    
    [self addSubview:textView];
    self.textView = textView;
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(15, 12, 45, 12));
    }];
    
    UIButton *cancel = [[UIButton alloc] init];
    cancel.backgroundColor = UIColorFromRGB(0xdbdbdb);
    cancel.titleLabel.font = [UIFont systemFontOfSize:15];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    cancel.layer.masksToBounds = YES;
    cancel.layer.cornerRadius = 13;
    [cancel setTitleColor:UIColorFromRGB(kAllWhiteColor)  forState:UIControlStateNormal];
    [cancel addTarget:self action:@selector(cancelComment) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancel];
    [cancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-8);
        make.right.offset(-110);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(27);
    }];
    
    UIButton *sendButton = [[UIButton alloc] init];
    sendButton.layer.masksToBounds = YES;
    sendButton.layer.cornerRadius = 13;
    sendButton.enabled = NO;
    [sendButton setBackgroundColor:UIColorFromRGB(0xff3030)];
    sendButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [sendButton setTitleColor:UIColorFromRGB(kAllWhiteColor)  forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(sendComment) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:sendButton];
    self.sendButton = sendButton;
    [sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-8);
        make.right.offset(-12);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(27);
    }];
}
#pragma mark UITextView Delegate

-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        self.sendButton.enabled = NO;
    } else {
        self.sendButton.enabled = YES;
    }

}

- (void)sendComment{
    if ([_delegate respondsToSelector:@selector(sendComment)]) {
         [_delegate sendComment];
    }
}

- (void)cancelComment{
    if ([_delegate respondsToSelector:@selector(cancelComment)]) {
        [_delegate cancelComment];
    }
}


@end
